package com.future

class TaskController {

  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
  def beforeInterceptor={
      def loginFlag=session.getAttribute("loginFlag")
      if(!loginFlag){
         redirect(controller:'login',action:'index')
        return false
      }
    }
  def googleCalendar={
    def taskInstance=Task.get(params.id)
    [taskInstance:taskInstance]
  }
  def updateGoogleCalendar={
    def taskInstance=Task.get(params.id)
    taskInstance.setGoogleCalendar(params.googleCalendar)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush:true)
    redirect(action:'list')
  }
  def index = {
    redirect(action: "list", params: params)
  }
  def TaskInfo={
    def taskInstance=Task.get(params.id)
    [taskInstance:taskInstance]
  }
  def updateTaskInfo={
    def taskInstance=Task.get(params.id)
    taskInstance.setTaskInfo (params.taskInfo)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush:true)
    redirect(action: "list")
  }
  def taskThink={
     def taskInstance=Task.get(params.id)
    [taskInstance:taskInstance]
  }
  def updateTaskThink={
    def taskInstance=Task.get(params.id)
    taskInstance.setTaskThink (params.taskThink)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush:true)
    redirect(action: "list")
  }
  def taskUpgrade={
    def taskInstance=Task.get(params.id)
    [taskInstance:taskInstance]
  }
  def updateTaskUpgrade={
    def taskInstance=Task.get(params.id)
    taskInstance.setTaskUpgrade (params.taskUpgrade)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush:true)
    redirect(action: "list")
  }
  def designed={
    def taskInstance=Task.get(params.id)
    [taskInstance:taskInstance]
  }
  def updateDesigned={
    def taskInstance=Task.get(params.id)
    taskInstance.setDesigned (params.designed)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush:true)
    redirect(action: "list")
  }
  def functionList={
    def taskInstance=Task.get(params.id)
    [taskInstance:taskInstance]
  }
  def updateFunctionList={
    def taskInstance=Task.get(params.id)
    taskInstance.setFunctionList (params.functionList)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush:true)
    redirect(action: "list")
  }
  def personalEvaluation={
    def taskInstance=Task.get(params.id)
    [taskInstance:taskInstance]
  }
  def updatePersonalEvaluation={
    def taskInstance=Task.get(params.id)
    taskInstance.setPersonalEvaluation (params.personalEvaluation)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush:true)
    redirect(action: "list")
  }
  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [taskInstanceList: Task.list(params), taskInstanceTotal: Task.count()]
  }

  def create = {
    def taskInstance = new Task()
    taskInstance.properties = params
    return [taskInstance: taskInstance]
  }

  def save = {
    def taskInstance = new Task(params)
    if (taskInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'task.label', default: 'Task'), taskInstance.id])}"
      redirect(action: "show", id: taskInstance.id)
    }
    else {
      render(view: "create", model: [taskInstance: taskInstance])
    }
  }
  def showImage={
        response.setContentType("image/png")
        OutputStream out=response.getOutputStream()
        if(params.id){
            def task=Task.get(params.id)
            if(task.taskPicture==null||task.taskPicture==""){
                java.io.File file=new File(servletContext.getRealPath("/images")+"/grails_logo.jpg")
                byte[] bytes=file.readBytes()
                out.write(bytes)
                out.close()
            }else{
                def image=task.taskPicture
                byte[] bytes=image
                out.write(bytes)
                out.close()
            }
        }else{
            java.io.File file=new File(servletContext.getRealPath("/images")+"/grails_logo.jpg")
            byte[] bytes=file.readBytes()
            out.write(bytes)
            out.close()
        }
    }
  def taskGantt={
    
  }
  def showUML={
        response.setContentType("image/png")
        OutputStream out=response.getOutputStream()
        if(params.id){
            def task=Task.get(params.id)
            if(task.taskUML==null||task.taskUML==""){
                println "没有上传"
                java.io.File file=new File(servletContext.getRealPath("/images")+"/grails_logo.jpg")
                byte[] bytes=file.readBytes()
                out.write(bytes)
                out.close()
            }else{
                def image=task.taskUML
                byte[] bytes=image
                out.write(bytes)
                out.close()
            }
        }else{
            java.io.File file=new File(servletContext.getRealPath("/images")+"/grails_logo.jpg")
            byte[] bytes=file.readBytes()
            out.write(bytes)
            out.close()
        }
    }
  def show = {
    def taskInstance = Task.get(params.id)
    if (!taskInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
      redirect(action: "list")
    }
    else {
      [taskInstance: taskInstance]
    }
  }
  def beginTask={
    def taskInstance=Task.get(params.id)
    def date=new Date()
    taskInstance.setTaskPlanBeginDate (date)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush: true)
    render(view:"show",model:[taskInstance:taskInstance])
  }
  def completeTask={
    def taskInstance=Task.get(params.id)
    def date=new Date()

    taskInstance.setTaskPlanCompleteDate (date)
    taskInstance.setFlag (1)//设置完成标志
    taskInstance.setTaskCompleteRate(1.0)//完成率100%
    taskInstance.save(flush:true)
    def factRate=(taskInstance.getTaskPlanCompleteDate()).getTime()-(taskInstance.getTaskPlanBeginDate()).getTime()
    taskInstance.setTaskFactCycle (factRate)
    taskInstance.setLastUpdate(new Date())
    taskInstance.save(flush:true)
    render(view:"show",model:[taskInstance:taskInstance])
  }
  def edit = {
    def taskInstance = Task.get(params.id)
    if (!taskInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
      redirect(action: "list")
    }else {
      return [taskInstance: taskInstance]
    }
  }

  def update = {
    def taskInstance = Task.get(params.id)
    if (taskInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (taskInstance.version > version) {
          taskInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'task.label', default: 'Task')] as Object[], "Another user has updated this Task while you were editing")
          render(view: "edit", model: [taskInstance: taskInstance])
          return
        }
      }
      taskInstance.properties = params
      taskInstance.lastUpdate=new Date()
      if (!taskInstance.hasErrors() && taskInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'task.label', default: 'Task'), taskInstance.id])}"
        redirect(action: "show", id: taskInstance.id)
      }
      else {
        render(view: "edit", model: [taskInstance: taskInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def taskInstance = Task.get(params.id)
    if (taskInstance) {
      try {
        taskInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'task.label', default: 'Task'), params.id])}"
      redirect(action: "list")
    }
  }
}
